#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;

using System.Web;

using System.Collections;
using WaveMaster.Holidays.Club.Domain;
using WaveMaster.Holidays.Club.Service;

#endregion

namespace WaveMaster.Holidays.Club.Web
{
    public partial class DepartureControlForm : Form
    {
        SeaPort m_user;
        User m_current_user;
        EditMode m_editmode;
        SeaPortService m_seaPortService;
        CheckInService m_bookingService;

        MainForm m_mainForm;
        PassportService m_passportService;
        TripService m_tripService;


        public DepartureControlForm(string name, SeaPort user, User currentUser, MainForm mainForm, EditMode editmode)
        {
            InitializeComponent();
            m_tripService = (TripService)ServiceFactory.Instance().GetService(ServiceFactory.TRIP_SERVICE);
            m_seaPortService = (SeaPortService)ServiceFactory.Instance().GetService(ServiceFactory.SEAPORT_SERVICE);
            m_bookingService = (CheckInService)ServiceFactory.Instance().GetService(ServiceFactory.BOOKING_SERVICE);
            initComboDataSource();
            m_current_user = currentUser;
            m_mainForm = mainForm;
            this.Name = name;

            initButtonEvent();

            setEditMode(editmode);
            //enableForm(false);
           
        }


        private void Valid()
        {
            textBoxMessage.Text = "VALID.";
            textBoxMessage.Text = String.Format("{0} VALID", textBoxBoardingPass.Text);
            textBoxMessage.BackColor = Color.Green;
            textBoxMessage.ForeColor = Color.White;
            textBoxBoardingPass.Text = "";
        }

        void Boarding()
        {
            try
            {
                if (isValidForm())
                {
                    
                    Trip trip = (Trip)comboBoxDepartureTrip.SelectedValue;
                    Passenger pass = m_bookingService.GetPassengerByBoardingPass(textBoxBoardingPass.Text);
                    if (pass != null)
                    {
                        m_bookingService.UpdatePassangerStatus(pass, PassengerStatus.Boarding);
                        Valid();
                    }
                    
                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        private void clearForm()
        {
            //textBoxName.Text = string.Empty;
            //textBoxUserCode.Text = string.Empty;
            //checkBoxActive.Checked = true;
            //comboBoxCountry.SelectedIndex = 0;
            //textBoxREmark.Text = "";
            //m_user = new SeaPort();
            //clearErrorProvider();
            //enableForm(true);
            //setEditMode(EditMode.Create);
            //textBoxUserCode.SelectAll();
        }

        private void initButtonEvent()
        {
            ////toolBarButtonClear.Click += new EventHandler(toolBarButtonClear_Click);
            //toolBarButtonSave.Click += new EventHandler(toolBarButtonSave_Click);
            ////toolBarButtonDelete.Click += new EventHandler(toolBarButtonDelete_Click);
            //toolBarButtonRefresh.Click += new EventHandler(toolBarButtonRefresh_Click);
            //toolBarButtonEdit.Click += new EventHandler(toolBarButtonEdit_Click);
            ////toolBarButtonSearch.Click += new EventHandler(toolBarButtonSearch_Click);
            //toolBarButtonCancel.Click += new EventHandler(toolBarButtonCancel_Click);
        }
              
        void toolBarButtonEdit_Click(object sender, EventArgs e)
        {
            setEditMode(EditMode.Edit);
        }

        void toolBarButtonCancel_Click(object sender, EventArgs e)
        {
            if (m_editmode == EditMode.Load)
                this.Close();
            else
            MessageBox.Show("Are you sure to cancel?", "Confirmation", MessageBoxButtons.YesNo,
               MessageBoxIcon.Question, new EventHandler(cancelConfirmation));
        }

        void cancelConfirmation(object sender, EventArgs e)
        {
             MessageBoxWindow msg = (MessageBoxWindow)sender;
             if (msg.DialogResult == DialogResult.Yes)
             {
                 this.Close();
             }
        }
        private void initComboDataSource()
        {
            comboBoxDepartureSeaPort.DataSource = m_current_user.UserType == UserType.Administrators ? m_seaPortService.LoadAllSeaPort() : m_seaPortService.LoadAllActiveSeaPort();
        }

        private void clearErrorProvider()
        {
            errorProviderUser.SetError(comboBoxDepartureTrip, "");
        }
        private bool isValidForm()
        {
            clearErrorProvider();
            bool valid_b = textBoxBoardingPass.Text != "";
            bool valid_a = comboBoxDepartureTrip.SelectedValue != null;
            if (!valid_a) errorProviderUser.SetError(comboBoxDepartureTrip, "Please choose Departure Trip");
            return valid_a && valid_b;
        }
        void closeForm(object sender, EventArgs e)
        {
            this.Close();
        }
        private void setEditMode(EditMode editMode)
        {
            toolBarButtonSave.Enabled = (editMode == EditMode.Create || editMode == EditMode.Edit || editMode == EditMode.Saved || editMode == EditMode.Delete)&& m_current_user.RoleDetails(this.Name).NewFlag;
            toolBarButtonEdit.Enabled = (editMode == EditMode.Load) && m_current_user.RoleDetails(this.Name).UpdateFlag;
            m_editmode = editMode;
            labelEditMode.Text = "[" + editMode + "]";
        }
        private void comboBoxDepartureSeaPort_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBoxDepartureTrip.DataSource = null;
            if (comboBoxDepartureSeaPort.SelectedValue == null) return;
            SeaPort port = (SeaPort)comboBoxDepartureSeaPort.SelectedValue;
            comboBoxDepartureTrip.DataSource = m_tripService.LoadAllTripByDateAndSeaPort(DateTime.Today, port.m_seaPortID);
        }

        private void buttonBOarding_Click(object sender, EventArgs e)
        {
            Boarding();
        }
    }
}